# task_master.py
import datetime
import platform
import queue
import time
from multiprocessing import Queue
from multiprocessing.managers import BaseManager

# 从BaseManager继承的QueueManager:
import facade
from pymongo import MongoClient
from xjlibrary.mdatetime.mtime2 import MDateTimeUtils
from xjlibrary.network.MyIP import GetLocalIPByPrefix, get_local_ip
from xjlibrary.our_file_dir import BaseDir

curPath = BaseDir.get_file_dir_absolute(__file__)
curPath = BaseDir.get_upper_dir(curPath, -1)
configfile = BaseDir.get_new_path(curPath, "db.ini")


class QueueManager(BaseManager):
    pass


class SendTask(object):
    def __init__(self):
        # 发送任务的队列:
        self.task_queue = Queue(10)
        # 接收结果的队列:
        self.result_queue = Queue()
        self.register()
        sysstr = platform.system()
        if sysstr == "Windows":
            self.ip = GetLocalIPByPrefix("192.168.")
        else:
            self.ip = get_local_ip("enp2s0")
        # 绑定端口5000, 设置验证码'abc':
        self.manager = QueueManager(address=(self.ip, 5001), authkey=b'abc')
        self.task = None
        self.result = None

    def register(self):
        # 把两个Queue都注册到网络上, callable参数关联了Queue对象:
        QueueManager.register('get_task_queue', callable=self.return_task_queue)
        QueueManager.register('get_result_queue', callable=self.return_result_queue)

    def return_task_queue(self):
        return self.task_queue

    def return_result_queue(self):
        return self.result_queue

    def start(self):
        # 启动Queue:
        self.manager.start()

    def set_task_result_obj(self):
        # 获得通过网络访问的Queue对象:
        self.task = self.manager.get_task_queue()
        self.result = self.manager.get_result_queue()

    def set_task(self, n):
        print(datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S.%f"))
        print(n)
        self.task.put(n)

    def get_result(self):
        # 从result队列读取结果:
        print('Try get results...')
        try:
            for i in range(10):
                r = self.result.get(timeout=10)
                print('Result: %s' % r)
        except queue.Empty:
            print('task queue is empty.')

    def shutdown(self):
        # 关闭:
        self.manager.shutdown()
        print('master exit.')


class SelectTask(object):
    def __init__(self):
        self.logger = facade.get_streamlogger()
        self.mysqlutils = facade.MysqlUtiles(configfile,
                                             "db",
                                             logger=self.logger)
        self.connection = MongoClient(
            "mongodb://xujiangrw:vipdatacenter@192.168.30.171:27017",
            # 如果“True”且服务器正在运行而没有日志记录，阻塞，直到服务器将所有数据文件同步到磁盘为止。
            fsync=False
        )
        self.db = self.connection['cnipa']

    def selectDB(self):
        """
        获取page表状态为0的数据
        :return:
        """
        updatetime = "updatetime"
        source = "mimvp"
        is_time = True
        timesleep = 150
        # 其中1为升序，-1为降序。
        rowss = self.db.cookies.find(
            {"stat": {"$in": [0, 1]}, "source": source, "origin": "http://pss-system.cnipa.gov.cn"},
            {"cookie": 1, "source": 1, "proxy": 1, "user": 1, "_id": 1, updatetime: 1},
            sort=[(updatetime, 1)])
        for rows in rowss:
            if is_time and rows:
                update_time = MDateTimeUtils.datetime_to_string(
                    MDateTimeUtils.get_seconds_before(datetime.datetime.utcnow() + datetime.timedelta(hours=8),
                                                      timesleep), "%Y%m%d%H%M%S")
                print(rows["proxy"])
                print(rows[updatetime])
                print(update_time)
                if int(rows[updatetime]) > int(update_time):
                    print("continue")
                    continue
            if rows:
                update_time = MDateTimeUtils.get_beijin_date_strins()
                self.db.cookies.update({"_id": rows["_id"], "source": rows["source"]},
                                       {"$set": {"stat": 1, updatetime: update_time}})

                self.logger.info("获取一个代理{}".format(rows["proxy"]))
                yield rows
            else:
                print("没有select到数据")


if __name__ == "__main__":
    sendtask = SendTask()
    select = SelectTask()
    sendtask.start()
    sendtask.set_task_result_obj()
    while True:
        num = sendtask.task.qsize()
        if num != 0:
            print(sendtask.task.qsize())
            time.sleep(20)
            print("continue")
            continue
        print("selectDB")
        for rows in select.selectDB():
            print("send a cookies")
            sendtask.set_task(rows)
        print(sendtask.task.qsize())
        print("sleep 30")
        time.sleep(80)
        print("sleep over")
